package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class BlogSpotCategoryPeer extends BasePeer{
	public static final String tableName = "_BlogSpot_Category";
	public static final String BLOGSPOTID = "blogSpotId";
	public static final String CATEGORYID = "categoryId";
	public static final String COUNT = "count";
	public static final String[] columnNames=new String[]{"blogSpotId","categoryId","count"};
	public static BlogSpotCategory getBlogSpotCategoryByPKs(Object blogSpotId,Object categoryId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(BlogSpotCategoryPeer.tableName,BlogSpotCategoryPeer.columnNames," TRUE  AND `blogSpotId`=?  AND `categoryId`=? ",new Object[]{blogSpotId,categoryId,},0, 1,conn);
			BlogSpotCategory blogSpotCategory = new BlogSpotCategory();
		fill(rs,blogSpotCategory);
		return blogSpotCategory;
	}
	public static BlogSpotCategory getBlogSpotCategoryReferenceColumnsOnlyByPKs(Object blogSpotId,Object categoryId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(BlogSpotCategoryPeer.tableName,new String[]{"blogSpotId","categoryId",}," TRUE AND `blogSpotId`=?AND `categoryId`=?",new Object[]{blogSpotId,categoryId,}, 0,  1,conn);
			BlogSpotCategory blogSpotCategory = new BlogSpotCategory();
		fill(rs,blogSpotCategory);
		return blogSpotCategory;
	}
	public static BlogSpotCategory getBlogSpotCategoryByBlogSpotId(long blogSpotId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(BlogSpotCategoryPeer.tableName,BlogSpotCategoryPeer.columnNames,"`blogSpotId`=?",new Object[]{blogSpotId},0, 1,conn);
			BlogSpotCategory blogSpotCategory = new BlogSpotCategory();
		fill(rs,blogSpotCategory);
		return blogSpotCategory;
	}
	public static BlogSpotCategory getBlogSpotCategoryByCategoryId(long categoryId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(BlogSpotCategoryPeer.tableName,BlogSpotCategoryPeer.columnNames,"`categoryId`=?",new Object[]{categoryId},0, 1,conn);
			BlogSpotCategory blogSpotCategory = new BlogSpotCategory();
		fill(rs,blogSpotCategory);
		return blogSpotCategory;
	}
	public static void fill(ResultSet rs, BlogSpotCategory blogSpotCategory) throws SQLException {
		if(rs.next()){
			try{blogSpotCategory.setBlogSpotId(rs.getLong("blogSpotId"));}catch(Exception ex){}
			try{blogSpotCategory.setCategoryId(rs.getLong("categoryId"));}catch(Exception ex){}
			try{blogSpotCategory.setCount(rs.getLong("count"));}catch(Exception ex){}
			blogSpotCategory.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<BlogSpotCategory> blogSpotCategoryCollection) throws SQLException {
		while(rs.next()){
			BlogSpotCategory blogSpotCategory=new BlogSpotCategory();
			try{blogSpotCategory.setBlogSpotId(rs.getLong("blogSpotId"));}catch(Exception ex){}
			try{blogSpotCategory.setCategoryId(rs.getLong("categoryId"));}catch(Exception ex){}
			try{blogSpotCategory.setCount(rs.getLong("count"));}catch(Exception ex){}
			blogSpotCategory.resetModified();
			blogSpotCategoryCollection.add(blogSpotCategory);
		}
	}
	public static boolean update(BlogSpotCategory blogSpotCategory, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < BlogSpotCategoryPeer.columnNames.length; i++) {
			if (((BlogSpotCategoryBase)blogSpotCategory).modified[i]) {
				columns.add(BlogSpotCategoryPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `_BlogSpot_Category` SET ");
		sql.append("WHERE TRUE AND `blogSpotId` = ? AND `categoryId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,blogSpotCategory.get(column));
		}
		pstmt.setObject(columns.size()+1+0,blogSpotCategory.get("blogSpotId"));
		pstmt.setObject(columns.size()+1+1,blogSpotCategory.get("categoryId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(BlogSpotCategory blogSpotCategory, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (blogSpotCategory.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `_BlogSpot_Category` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (blogSpotCategory.get(column) != null) {
				pstmt.setObject(i + 1 + j, blogSpotCategory.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<BlogSpotCategory> getBlogSpotCategoryCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<BlogSpotCategory> collection=new Vector<BlogSpotCategory>();
		fill(rs,collection);
		return collection;
	}
}
